﻿Imports System.IO

Module Foutlogger

    Public Sub Loggen(ex As Exception)
        Dim folder As Environment.SpecialFolder
        Dim folderpad As String
        folder = Environment.SpecialFolder.ApplicationData
        folderpad = String.Concat(Environment.GetFolderPath(folder), "\Chiroredactie\foutlog.txt")
        NaarLogBestandSchrijven(folderpad, ex)
    End Sub

    Private Sub NaarLogBestandSchrijven(ByVal PadParam As String, ByVal FoutParam As Exception)
        ' Initialiseer bestandstoegang
        Dim file As New StreamWriter(PadParam, True)

        ' Maak logitem aan
        With file
            .WriteLine("------------------------")
            .WriteLine(DateTime.Now.ToString)
            .WriteLine("------------------------")

            .WriteLine("Exception zelf")
            .WriteLine("------------------------")

            .WriteLine(String.Format("Type exception:{0}{1}", vbTab, FoutParam.GetType.ToString))
            .WriteLine(String.Format("Message:{0}{1}", vbTab, FoutParam.Message.ToString))
            .WriteLine(String.Format("Stacktrace:{0}{1}", vbTab, FoutParam.StackTrace.ToString))
            .WriteLine(String.Format("TargetSite:{0}{1}", vbTab, FoutParam.TargetSite.Name))
            .WriteLine(String.Format("Source:{0}{1}", vbTab, FoutParam.Source.ToString))
            .WriteLine("------------------------")

            If Not FoutParam.InnerException Is Nothing Then
                .WriteLine("InnerException")
                .WriteLine("------------------------")

                .WriteLine(String.Format("InnerException:{0}{1}", vbTab, FoutParam.InnerException.ToString))
                .WriteLine(String.Format("InnerExceptionMessage:{0}{1}", vbTab, _
                                         FoutParam.InnerException.Message.ToString))
                .WriteLine(String.Format("InnerException StackTrace:{0}{1}", vbTab, _
                                         FoutParam.InnerException.StackTrace.ToString))
                .WriteLine(String.Format("InnerException TargetSite:{0}{1}", vbTab, _
                                         FoutParam.InnerException.TargetSite.ToString))
                .WriteLine("------------------------")
            End If

            .Close()
        End With
    End Sub

End Module
